<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('定时任务列表')" />
    <style type="text/css">
        .layui-col-sm4 {width: 23%;}
    </style>
</head>
<body>
<div class="main-container" id="main-container">
		<div class="main-content">
           <form class="layui-form dis-ctr" >
               <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                   <div class="layui-form-item">
                       <div class="layui-inline layui-col-sm4">
                           <label class="layui-form-label">任务名称：</label>
                           <div class="layui-input-block">
                               <input id="jobName" type="text" name="vname" autocomplete="off" class="layui-input">
                           </div>
                       </div>
                       <div class="layui-inline layui-col-sm4">
                           <label class="layui-form-label">任务分组：</label>
                           <div class="layui-input-block">
                               <select id="jobGroup">
                                   <option value="">所有</option>
                                   <option value="DEFAULT">默认</option>
                                   <option value="SYSTEM">系统</option>
                               </select>
                           </div>
                       </div>
                       <div class="layui-inline layui-col-sm4">
                           <label class="layui-form-label">任务状态：</label>
                           <div class="layui-input-block">
                               <select id="status">
                                   <option value="">所有</option>
                                   <option value="0">正常</option>
                                   <option value="1">暂停</option>
                               </select>
                           </div>
                       </div>
                       <div class="layui-inline layui-col-sm4">
                           <a class="layui-btn layuiadmin-btn-order dhicon dh-search" id="btn-search"><span class="fa_span">查询</span></a>
                           <button type="reset" class="layui-btn layui-btn-primary dhicon dh-refresh" id="dh-resets"><span class="fa_span">重置</span></button>
                       </div>
                   </div>
               </div>
           </form>
       </div>
    <div class="mid-content">
        <table class="layui-hide" id="dhTable" lay-filter="ftable"></table>
    </div>
</div>
<script type="text/html" id="toolbar">
	<div class="layui-btn-container">
		<a class="layui-btn layui-btn-normal layui-table-tools" onclick="$.operate.add()" shiro:hasPermission="common:job:add">
			<i class="dhicon dh-plus"></i> 新增
		</a>
		<a class="layui-btn layui-table-tools" onclick="$.tablelist.exportExcel()" shiro:hasPermission="common:job:export">
    		<i class="dhicon dh-download"></i> 导出
		</a>
		<a class="layui-btn layui-btn-warm layui-table-tools" onclick="javascript:jobLog()" shiro:hasPermission="common:job:list">
		    <i class="dhicon dh-list"></i> 日志
		</a>
        <a class="layui-btn layui-table-tools" onclick="javascript:cron()" shiro:hasPermission="common:job:list">
            <i class="dhicon dh-file-code-o"></i> 生成表达式
        </a>
    </div>
</script>
<script type="text/html" id="barDh">
	<a class="layui-btn layui-btn-xs dhicon dh-play-circle-o layui-btn-normal" href="javascript:void(0)" onclick="run('{{d.jobId}}','{{d.jobGroup}}')" shiro:hasPermission="common:job:edit"> 执行一次</a>
	<a class="layui-btn layui-btn-xs dhicon dh-edit" onclick="$.operate.edit('{{d.jobId}}')" shiro:hasPermission="common:job:edit"> 修改</a>
	<a class="layui-btn layui-btn-xs dhicon dh-remove layui-btn-danger" onclick="remove('{{d.jobId}}')" shiro:hasPermission="common:job:delete"> 删除</a>
	<a class="layui-btn layui-btn-xs dhicon dh-search layui-btn-warm" href="javascript:void(0)" onclick="$.operate.detail('{{d.jobId}}')" shiro:hasPermission="common:job:view"> 详细</a>
</script>
<script type="text/html" id="status1">
    <input type="checkbox" jobId="{{d.jobId}}" jobGroup="{{d.jobGroup}}" lay-skin="switch" lay-text="正常|暂停" lay-filter="mdState" {{ d.status=== '0' ? 'checked' : '' }}>
</script>
<th:block th:include="include :: footer" />
<th:block th:include="include :: motif-alone" />
<script th:inline="javascript">
	/*<![CDATA[*/
	var prefix = ctx + "common/job";
	/*]]>*/
</script>
<script type="text/javascript">
var $,layer,form,table,wherestr={};
layui.use(['table','form','layer','jquery'], function () {
     $ = layui.$,layer = layui.layer,form = layui.form,table = layui.table;
     $.tablelist.init({
         elem: '#dhTable'
         ,createUrl: prefix + "/add"
         ,updateUrl: prefix + "/edit/{id}"
         ,detailUrl: prefix + "/detail/{id}"
         ,exportUrl: prefix + "/export"
	     ,modalName: "任务"
         ,url: prefix + "/list"
         ,initSort: {field: 'createTime',type: 'desc'}
         ,where: {
             field: 'createTime' //排序字段
             ,order: 'desc' //排序方式
         }
         ,cols: [[
            {type: 'radio'},
 	        {field: 'jobId',title: '任务编号', sort: true, width:100},
 	        {field: 'jobName',title: '任务名称', sort: true},
 	        {field: 'jobGroup',title: '任务分组', width:100,formatter: function(value, row, index) {
 	            	return $.table.selectDictLabel(datas, value);
 	            }
 	        },
 	        {field: 'invokeTarget',title: '调用目标字符串',formatter: function(value, row, index) {
 	            	return $.table.tooltip(value);
 	            }
 	        },
 	        {field: 'cronExpression',title: '执行表达式'},
 	        {title: '任务状态',align: 'center',templet: '#status1', sort: true, width:120},
 	        {field: 'createTime',title: '创建时间', sort: true, width:150},
 	        {title: '操作',width:350,align: 'center',templet:'#barDh'}
 	    ]
     	]
        ,toolbar: '#toolbar'
        ,height:'full-' + ( $('.north').height()+70),
     });
    //监听排序事件
    table.on('sort(ftable)', function(obj){
     	$.tablelist.sort('dhTable',obj);
    });
    //单击选中行
    table.on('row(ftable)', function(obj){
        obj.tr.find('input[lay-type="layTableRadio"]').prop("checked",true);
        layui.each(table.cache.dhTable, function(i, item){//重置数据单选属性
            if(obj.tr.data('index') === i){
                item.LAY_CHECKED = true;
            } else {
                delete item.LAY_CHECKED;
            }
        });
        form.render('radio');
    });
	//监听开关动作
 	form.on('switch(mdState)', function(obj){
   	    if(obj.elem.checked==true){
		    obj.elem.checked=false;
		    layui.form.render();
			$.modal.confirmCom("确认要启用任务吗？", function() {/* 调度任务-启用 */
				$.operate.post(prefix + "/changeStatus", { "jobId": obj.elem.attributes.jobId.value, "jobGroup": obj.elem.attributes.jobGroup.value, "status": 0 });
				obj.elem.checked=true;
			    layui.form.render();
			})
 	    }else{
 		    obj.elem.checked=true;
 		    layui.form.render();
 		   	$.modal.confirmCom("确认要停用任务吗？", function() {/* 调度任务-停用 */
				$.operate.post(prefix + "/changeStatus", { "jobId": obj.elem.attributes.jobId.value, "jobGroup": obj.elem.attributes.jobGroup.value, "status": 1 });
				obj.elem.checked=false;
			    layui.form.render();
			})
	  	}
    });
	//表单重载方法
	var active = {
        reload: function (formData) {
        	wherestr = {
				jobName: $("#jobName").val(),
				jobGroup: $("#jobGroup").val(),
				status: $("#status").val()
			};
            table.reload('dhTable', {
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: wherestr
            });
        }
    };
	//搜索
	$('#btn-search').on('click', function () {
        active['reload'].call(this);
    });
});
//删除
function remove(jobId){
	$.operate.remove("确定要删除么？",prefix + "/remove",{'ids':jobId},function(result){
		allReload(result)
	});
}
//刷新
function allReload(result){
    if(result.code==0){
        setTimeout(function(){
            layer.closeAll();
            table.reload('dhTable');
        }, 1000);
    }
}
/* 立即执行一次 */
function run(jobId, jobGroup) {
	$.modal.confirmCom("确认要立即执行一次任务吗？", function() {
		$.operate.post(prefix + "/run", {"jobId": jobId, "jobGroup": jobGroup});
	})
}
/* 调度日志查询 */
function jobLog() {
    if(!table.checkStatus('dhTable').data.length){
    	$.modal.msgWarning("请选择！");
    }else{
        var row =table.checkStatus('dhTable').data[0];
        var id = row.jobId;
        var title = "[" + row.jobName + "]调度日志";
        var url = ctx + 'common/jobLog?jobId='+id;
        top.layui.index.openTabsPage(url,title);
    }
}
/* cron表达式生成 */
function cron() {
    var url = prefix + '/cron';
    var height = $(window).height() - 50;
    top.layer.open({
        maxmin: true,
        title: "Cron表达式生成器",
        type: 2,
        area: ['800px', height + "px" ], //宽高
        shadeClose: true,
        content: url
    });
}
</script>
</body>
</html>